package org.dromara.common.mqtt.enums;

import lombok.Getter;

import java.util.Arrays;
import java.util.regex.Pattern;

@Getter
public enum CloudApiTopicEnum {
    STATUS(Pattern.compile("elevator/status/.+"), "statusMessageHandler"),
    SMOKE(Pattern.compile("elevator/event/.+/smoke"), "smokeMessageHandler"),
    FIRE(Pattern.compile("elevator/event/.+/fire"), "fireMessageHandler"),
    FORBIDDEN_ITEM(Pattern.compile("elevator/event/.+/forbidden_item"), "forbiddenItemMessageHandler"),
    BEHAVIOR(Pattern.compile("elevator/event/.+/behavior"), "behavior"),
    personCount(Pattern.compile("elevator/event/.+/person_count"), "personCountHandler");

    private final Pattern mqttTopic;
    private final String beanName;

    CloudApiTopicEnum(Pattern mqttTopic, String beanName) {
        this.mqttTopic = mqttTopic;
        this.beanName = beanName;
    }

    public static CloudApiTopicEnum getEnumByTopic(String topic) {
        return Arrays.stream(CloudApiTopicEnum.values()).filter(e -> e.getMqttTopic().matcher(topic).matches()).findFirst().orElse(null);
    }

}
